import { observable, action } from 'mobx';
import { WeaLogView } from 'comsMobx';

import ListBaseStore from './ListBaseStore';
import * as ItemMonitorApis from '../apis/itemMonitor';
import { WeaLocaleProvider } from 'ecCom';

const { LogStore } = WeaLogView;
const getLabel = WeaLocaleProvider.getLabel;

/**
 * 主题监控Store
 */
export default class ItemMonitorStore extends ListBaseStore {
	@observable logStore = new LogStore();

	constructor() {
		super();
		this.title = getLabel(32576, '主题监控');
		this.selectedKey = 'all';
	}

	@observable status = {
		targetId: '',
		logType: '', // 日志相关参数
		logSmallType: '',
	}

	@action doInit = () => {
    this.doBaseInit();
    this.selectedKey = 'all';
    this.doTableLoading(); // 初始化列表
    this.initConditionDatas(); // 初始化高级搜索区域
    this.loadLeftTree(); // 初始化左侧树
	}

	// 重置页面状态
	@action clearStatus = () => {
		this.setShowSearchAd(false);
		this.clearFormFields();
		this.selectedKey = 'all';
		this.selectedTreeKeys = [];
		this.viewRightInitCompleted = false;
	}

	// 加载列表数据
	@action doTableLoading = (params, initPage = false) => {
		this.showLoading(true);

		let resultParams = this.getFormParams();

		let selectedTreeKey = this.selectedTreeKeys.length > 0 ? this.selectedTreeKeys[0] : '';
		let key = selectedTreeKey ? selectedTreeKey.substring(selectedTreeKey.indexOf('_') + 1) : '';

		let selectedKey = this.selectedKey ? this.selectedKey : 'all';

		let commonParams = {
			mainid: (selectedTreeKey && selectedTreeKey.indexOf('cMainType_') != -1) ? key : '', // 协作类型
			datetype: selectedKey, // 时间类型
		};
		resultParams = { ...resultParams, ...commonParams, ...params };

		ItemMonitorApis.getCoworkItemMonitorList(resultParams).then(
			(result) => {
				this.status = { ...this.status, logType: result.logType, logSmallType: result.logSmallType };
				this.viewRight = result.viewRight == 1;
				if (this.viewRight) {
					const sessionkey = result.sessionkey;
          this.sessionkey = sessionkey;
          initPage ? this.comsWeaTableStore.getDatas(sessionkey) :
					this.comsWeaTableStore.getDatas(sessionkey, 1);
				}

				this.setViewRightInitCompleted(); // 要放在后面，防止有权限时，初始化慢导致无权限提示闪一下

				this.showLoading(false);
			},
		);
	}

	// 初始化收藏的帖子页面查询条件
	@action initConditionDatas = (params) => {
		ItemMonitorApis.getCoworkItemMonitorShareCondition(params).then(
			(result) => {
				const { conditioninfo, topTab } = result;
				this.topTab = topTab;
				this.conditioninfo = conditioninfo;
				this.initFormFields(conditioninfo); // 获取条件后一定要先调用一次初始化
			},
		);
	}

	// 加载左侧 版块树
	@action loadLeftTree = (params) => {
		ItemMonitorApis.getCoworkTreeDatas(params).then(
			(result) => {
				this.leftTreeDatas = result.treeDatas;
				this.leftTreeTypes = result.treeTypes;
				this.leftTreeCounts = result.treeCounts;
			},
		);
	}

	// 删除
	@action batchCoworkItemMonitorDel = (params, callback) => {
		ItemMonitorApis.batchCoworkItemMonitorDel(params).then(
			() => {
				if (callback && typeof callback === 'function') {
					callback();
				}
			},
		);
	}

	// 结束
	@action batchCoworkItemMonitorEnd = (params, callback) => {
		ItemMonitorApis.batchCoworkItemMonitorEnd(params).then(
			() => {
				if (callback && typeof callback === 'function') {
					callback();
				}
			},
		);
	}

	// 置顶
	@action batchCoworkItemMonitorTop = (params, callback) => {
		ItemMonitorApis.batchCoworkItemMonitorTop(params).then(
			() => {
				if (callback && typeof callback === 'function') {
					callback();
				}
			},
		);
	}

	// 取消置顶
	@action batchCoworkItemMonitorCancelTop = (params, callback) => {
		ItemMonitorApis.batchCoworkItemMonitorCancelTop(params).then(
			() => {
				if (callback && typeof callback === 'function') {
					callback();
				}
			},
		);
	}

	// 重新打开
	@action reOpenCowork = (params, callback) => {
		ItemMonitorApis.reOpenCowork(params).then(
			() => {
				if (callback && typeof callback === 'function') {
					callback();
				}
			},
		);
	}
}
